package com.ruoyi.threeViolation.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.mapper.SysDictItemMapper;
import com.ruoyi.threeViolation.domain.SysUser;
import com.ruoyi.threeViolation.domain.ViolationAssessment;
import com.ruoyi.threeViolation.domain.ViolationEducating;
import com.ruoyi.threeViolation.dto.ViolationExamineDto;
import com.ruoyi.threeViolation.dto.ViolationRegisterDto;
import com.ruoyi.threeViolation.mapper.SysUserMapper;
import com.ruoyi.threeViolation.mapper.ViolationAssessmentMapper;
import com.ruoyi.threeViolation.mapper.ViolationEducatingMapper;
import com.ruoyi.threeViolation.vo.ViolationExamineVo;
import com.ruoyi.threeViolation.vo.ViolationRegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.threeViolation.mapper.ViolationRegisterMapper;
import com.ruoyi.threeViolation.domain.ViolationRegister;
import com.ruoyi.threeViolation.service.IViolationRegisterService;

/**
 * 三违登记记录Service业务层处理
 *
 * @author lth
 * @date 2023-12-18
 */
@Service
public class ViolationRegisterServiceImpl implements IViolationRegisterService
{
    @Autowired
    private ViolationRegisterMapper violationRegisterMapper;

    @Autowired
    private ViolationEducatingMapper violationEducatingMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ViolationAssessmentMapper violationAssessmentMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;




    /**
     * 查询三违登记记录
     *
     * @param id 三违登记记录主键
     * @return 三违登记记录
     */
    @Override
    public ViolationRegister selectViolationRegisterById(String id)
    {
        return violationRegisterMapper.selectViolationRegisterById(id);
    }

    /**
     * 查询三违登记记录列表
     *
     * @param violationRegister 三违登记记录
     * @return 三违登记记录
     */
    @Override
    public List<ViolationRegister> selectViolationRegisterList(ViolationRegister violationRegister)
    {
        return violationRegisterMapper.selectViolationRegisterList(violationRegister);
    }

    /**
     * 新增三违登记记录
     *
     * @param violationRegister 三违登记记录
     * @return 结果
     */
    @Override
    public int insertViolationRegister(ViolationRegister violationRegister)
    {
        return violationRegisterMapper.insertViolationRegister(violationRegister);
    }

    /**
     * 修改三违登记记录
     *
     * @param violationRegister 三违登记记录
     * @return 结果
     */
    @Override
    public int updateViolationRegister(ViolationRegister violationRegister)
    {
        return violationRegisterMapper.updateViolationRegister(violationRegister);
    }

    /**
     * 批量删除三违登记记录
     *
     * @param ids 需要删除的三违登记记录主键
     * @return 结果
     */
    @Override
    public int deleteViolationRegisterByIds(String[] ids)
    {
        return violationRegisterMapper.deleteViolationRegisterByIds(ids);
    }

    /**
     * 删除三违登记记录信息
     *
     * @param id 三违登记记录主键
     * @return 结果
     */
    @Override
    public int deleteViolationRegisterById(String id)
    {
        return violationRegisterMapper.deleteViolationRegisterById(id);
    }



    /**     查询三违审核列表
     * @Author lth
     * @Description //TODO
     * @Date 19:46 2023/12/18
     * @Param
     * @return
     **/
    @Override
    public List<ViolationRegisterDto> auditList(ViolationRegisterVo vo) {
        List<ViolationRegister> violationRegisterList = violationRegisterMapper.selectViolationRegisterList(null);

        List<ViolationRegisterDto> dtoList = violationRegisterList.stream().map(violationRegister -> {
            ViolationRegisterDto violationRegisterDto = new ViolationRegisterDto();
            BeanUtils.copyProperties(violationRegister,violationRegisterDto);

            //volationShiftName
            String volationShift = violationRegister.getVolationShift();
            if (volationShift!=null){
                SysDictItem sysDictItem = sysDictItemMapper.getVolationShiftName(volationShift);
                violationRegisterDto.setVolationShiftName(sysDictItem.getItemText());
            }else {
                violationRegisterDto.setVolationShiftName(null);
            }
            //volationCategoryName
            String volationCategory = violationRegister.getVolationCategory();
            if (volationCategory!=null){
                SysDictItem category = sysDictItemMapper.getCategoryList(volationCategory);
                violationRegisterDto.setVolationCategoryName(category.getItemText());
            }else {
                violationRegisterDto.setVolationCategoryName(null);
            }
            //volationLevelName
            String volationLevel = violationRegister.getVolationLevel();
            if (volationLevel!=null){
                SysDictItem level = sysDictItemMapper.getLevelList(volationLevel);
                violationRegisterDto.setVolationLevelName(level.getItemText());
            }else {
                violationRegisterDto.setVolationLevelName(null);
            }


            String registerId = violationRegister.getId();
            //volation_educating
            ViolationEducating violationEducating = violationEducatingMapper.getByRegisterId(registerId);
            if (violationEducating != null) {
                BeanUtils.copyProperties(violationEducating, violationRegisterDto);
                //eduUserName
                String eduUserId = violationEducating.getEduUserId();
                if (eduUserId!=null){
                    SysUser sysUser = sysUserMapper.getById(eduUserId);
                    violationRegisterDto.setEduUserName(sysUser.getRealname());
                }else {
                    violationRegisterDto.setEduUserName(null);
                }

            }

            //assessment
            ViolationAssessment violationAssessment = violationAssessmentMapper.getByViolationId(registerId);
            if (violationAssessment != null) {
                BeanUtils.copyProperties(violationAssessment, violationRegisterDto);
            }

            //volationUserName
            String volationUser = violationRegister.getVolationUser();
            SysUser sysUser = sysUserMapper.getById(volationUser);
            violationRegisterDto.setVolationUserName(sysUser.getRealname());

            return violationRegisterDto;
        }).collect(Collectors.toList());

        return dtoList;
    }
}
